package com.hw.paltfrom.service.system;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hw.paltfrom.mapper.HwBaseOverrunstationMapper;
import com.hw.paltfrom.pojo.HwBaseOverrunstation;
import com.hw.paltfrom.rescode.HWSystemCode;
import com.hw.paltfrom.utils.LanguageUtil;
import com.hw.paltfrom.utils.MapRemoveNullUtil;
import com.hw.paltfrom.utils.RandomNumberUtil;
import com.hw.paltfrom.utils.ResponeResultUtil;
import com.hw.paltfrom.vo.BaseOverrunstationVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 超限检测站管理
 * 梁天
 * 2019/11/06
 *
 * */
@Service
public class BaseOverrunstationServer {

    @Autowired
    HwBaseOverrunstationMapper hwBaseOverrunstationMapper;


    public Map<String, Object> queryAllList(HashMap<String, Object> vo) {

        int pageName = Integer.parseInt(vo.get("pageNum").toString());
        int pageSize = Integer.parseInt(vo.get("pageSize").toString());
        PageHelper.startPage(pageName, pageSize);

        vo.remove("pageNum");
        vo.remove("pageSize");
        MapRemoveNullUtil.removeNullEntry(vo);

        Wrapper<HwBaseOverrunstation> namewrapper = new EntityWrapper<>();

        namewrapper.allEq(vo).and().eq("F_DeleteMark",0).orderBy("F_CreateDate");

        List<HwBaseOverrunstation> hwBaseOverrunstationList = hwBaseOverrunstationMapper.selectList(namewrapper);
        PageInfo<HwBaseOverrunstation> info = new PageInfo<>(hwBaseOverrunstationList);
        return ResponeResultUtil.successNotice(HWSystemCode.SUCCESS, info);
    }




    public Map<String, Object> insert(BaseOverrunstationVO vo) {

        // 查看是否名称重复
        Wrapper<HwBaseOverrunstation> namewrapper = new EntityWrapper<>();
        namewrapper.eq("F_OverrunName", vo.getfOverrunname()).and().eq("F_DeleteMark", 0);
        List<HwBaseOverrunstation> selectList = hwBaseOverrunstationMapper.selectList(namewrapper);
        if (!selectList.isEmpty()) {
            return ResponeResultUtil.fail(HWSystemCode.OFFSITENAME_EXIT);
        }

        HwBaseOverrunstation hwBaseOverrunstation = new HwBaseOverrunstation();
        hwBaseOverrunstation.setfOverrunid(RandomNumberUtil.getNumber());
        hwBaseOverrunstation.setfAreaid(vo.getfAreaid());
//        hwBaseOverrunstation.setfOverruncode(RandomNumberUtil.getCode());
        hwBaseOverrunstation.setfOverruncode(vo.getfOverruncode());
        hwBaseOverrunstation.setfOverrunname(vo.getfOverrunname());
        hwBaseOverrunstation.setfQuickquery(LanguageUtil.toHanyuPinyin(vo.getfOverrunname()));
        hwBaseOverrunstation.setfSimplespelling(vo.getfSimplespelling());
        hwBaseOverrunstation.setfLongitude(vo.getfLongitude());
        hwBaseOverrunstation.setfLatitude(vo.getfLatitude());
        hwBaseOverrunstation.setfSortcode(vo.getfSortcode());
        hwBaseOverrunstation.setfDeletemark(0);
        hwBaseOverrunstation.setfEnabledmark(1);
        hwBaseOverrunstation.setfDescription(vo.getfDescription());
        hwBaseOverrunstation.setfCreatedate(new Timestamp(System.currentTimeMillis()));
        hwBaseOverrunstation.setfCreateuserid(vo.getfCreateuserid());
        hwBaseOverrunstation.setfCreateusername(vo.getfCreateusername());
        hwBaseOverrunstation.setfModifydate(new Timestamp(System.currentTimeMillis()));
        hwBaseOverrunstation.setfModifyuserid(vo.getfModifyuserid());
        hwBaseOverrunstation.setfModifyusername(vo.getfModifyusername());
        hwBaseOverrunstation.setfAddress(vo.getfAddress());
        hwBaseOverrunstation.setfManager(vo.getfManager());
        hwBaseOverrunstation.setfTel(vo.getfTel());
        hwBaseOverrunstation.setfPic(vo.getfPic());
        hwBaseOverrunstation.setfSecManager(vo.getfSecManager());
        hwBaseOverrunstation.setfType(vo.getfType());
        hwBaseOverrunstation.setfStaCode(vo.getfStaCode());
        hwBaseOverrunstation.setfWorkerNum(vo.getfWorkerNum());

        //11.20新增
        hwBaseOverrunstation.setfDbld(vo.getfDbld());
        hwBaseOverrunstation.setfDbldphone(vo.getfDbldphone());
        hwBaseOverrunstation.setfDbldZfzh(vo.getfDbldZfzh());
        hwBaseOverrunstation.setfDbldCreatetime(new Timestamp(System.currentTimeMillis()));
        hwBaseOverrunstation.setfVideourl(vo.getfVideourl());
        hwBaseOverrunstation.setfVideouser(vo.getfVideouser());
        hwBaseOverrunstation.setfVideopassword(vo.getfVideopassword());
        hwBaseOverrunstation.setfVediochannel(vo.getfVediochannel());
        hwBaseOverrunstation.setfVediochannelname(vo.getfVediochannelname());

        Integer insert = hwBaseOverrunstationMapper.insert(hwBaseOverrunstation);
        if (insert != 1) {
            return ResponeResultUtil.fail(HWSystemCode.FAIL);
        }
        return ResponeResultUtil.success(HWSystemCode.SUCCESS);
    }



    public Map<String, Object> update(BaseOverrunstationVO vo) {

        // 通过ID查询待更新的数据
        Wrapper<HwBaseOverrunstation> wrapper = new EntityWrapper<>();
        wrapper.eq("F_OverrunID", vo.getfOverrunid());

        Integer fDeleteMark=vo.getfDeletemark();
        HwBaseOverrunstation hwBaseOverrunstation = new HwBaseOverrunstation();

        //如果删除标记为1的话，当前是删除操作
        if(fDeleteMark==1){
            //执行逻辑删除
            hwBaseOverrunstation.setfDeletemark(fDeleteMark);
            //记录是谁删除的
            hwBaseOverrunstation.setfModifydate(new Timestamp(System.currentTimeMillis()));
            hwBaseOverrunstation.setfModifyuserid(vo.getfModifyuserid());
            hwBaseOverrunstation.setfModifyusername(vo.getfModifyusername());
            Integer update = hwBaseOverrunstationMapper.update(hwBaseOverrunstation, wrapper);
            if (update != 1) {
                return ResponeResultUtil.fail(HWSystemCode.FAIL);
            }
        }else{//更新数据库
            // 查看是否名称重复
            Wrapper<HwBaseOverrunstation> namewrapper = new EntityWrapper<>();
            namewrapper.eq("F_OverrunName", vo.getfOverrunname()).and().notIn("F_OverrunID", vo.getfOverrunid());
            List<HwBaseOverrunstation> selectList = hwBaseOverrunstationMapper.selectList(namewrapper);
            if (!selectList.isEmpty()) {
                return ResponeResultUtil.fail(HWSystemCode.OFFSITENAME_EXIT);
            }

            hwBaseOverrunstation.setfAreaid(vo.getfAreaid());
            hwBaseOverrunstation.setfOverruncode(vo.getfOverruncode());
            hwBaseOverrunstation.setfOverrunname(vo.getfOverrunname());
            hwBaseOverrunstation.setfQuickquery(LanguageUtil.toHanyuPinyin(vo.getfOverrunname()));
            hwBaseOverrunstation.setfSimplespelling(vo.getfSimplespelling());
            hwBaseOverrunstation.setfDeletemark(fDeleteMark);
            hwBaseOverrunstation.setfDescription(vo.getfDescription());
            hwBaseOverrunstation.setfModifydate(new Timestamp(System.currentTimeMillis()));
            hwBaseOverrunstation.setfModifyuserid(vo.getfModifyuserid());
            hwBaseOverrunstation.setfModifyusername(vo.getfModifyusername());
            hwBaseOverrunstation.setfLongitude(vo.getfLongitude());
            hwBaseOverrunstation.setfLatitude(vo.getfLatitude());
            hwBaseOverrunstation.setfSortcode(vo.getfSortcode());
            hwBaseOverrunstation.setfEnabledmark(vo.getfEnabledmark());
            hwBaseOverrunstation.setfAddress(vo.getfAddress());
            hwBaseOverrunstation.setfManager(vo.getfManager());
            hwBaseOverrunstation.setfTel(vo.getfTel());
            hwBaseOverrunstation.setfPic(vo.getfPic());
            hwBaseOverrunstation.setfSecManager(vo.getfSecManager());
            hwBaseOverrunstation.setfType(vo.getfType());
            hwBaseOverrunstation.setfStaCode(vo.getfStaCode());
            hwBaseOverrunstation.setfWorkerNum(vo.getfWorkerNum());

            //11.20新增
            hwBaseOverrunstation.setfDbld(vo.getfDbld());
            hwBaseOverrunstation.setfDbldphone(vo.getfDbldphone());
            hwBaseOverrunstation.setfDbldZfzh(vo.getfDbldZfzh());
            hwBaseOverrunstation.setfDbldCreatetime(new Timestamp(System.currentTimeMillis()));
            hwBaseOverrunstation.setfVideourl(vo.getfVideourl());
            hwBaseOverrunstation.setfVideouser(vo.getfVideouser());
            hwBaseOverrunstation.setfVideopassword(vo.getfVideopassword());
            hwBaseOverrunstation.setfVediochannel(vo.getfVediochannel());
            hwBaseOverrunstation.setfVediochannelname(vo.getfVediochannelname());


            Integer update = hwBaseOverrunstationMapper.update(hwBaseOverrunstation, wrapper);
            if (update != 1) {
                return ResponeResultUtil.fail(HWSystemCode.FAIL);
            }
        }
        return ResponeResultUtil.success(HWSystemCode.SUCCESS);
    }


    public Map<String, Object> updateDb(BaseOverrunstationVO vo) {

        HwBaseOverrunstation hwBaseOverrunstation = hwBaseOverrunstationMapper.selectById(vo.getfOverrunid());

        hwBaseOverrunstation.setfDbld(vo.getfDbld());
        hwBaseOverrunstation.setfDbldphone(vo.getfDbldphone());
        hwBaseOverrunstation.setfDbldZfzh(vo.getfDbldZfzh());
        hwBaseOverrunstation.setfDbldCreatetime(new Timestamp(System.currentTimeMillis()));

        Integer update = hwBaseOverrunstationMapper.updateById(hwBaseOverrunstation);
        if (update != 1) {
            return ResponeResultUtil.fail(HWSystemCode.FAIL);
        }

        return ResponeResultUtil.success(HWSystemCode.SUCCESS);
    }
}